package com.hgt.cloudAlibaba.service.impl;

import com.hgt.cloudAlibaba.dao.OrderDao;
import com.hgt.cloudAlibaba.domain.Order;
import com.hgt.cloudAlibaba.service.AccountService;
import com.hgt.cloudAlibaba.service.OrderService;
import com.hgt.cloudAlibaba.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author HuangGaoTao 929507053@qq.com
 * @Created: 2021/7/21 17:17
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private AccountService accountService;

    @Autowired
    private StorageService storageService;


    /**
     * 创建订单->调用库存服务扣减库存->调用账户服务扣减账户余额->修改订单状态
     * */
    @GlobalTransactional(name = "hgt-create-order",rollbackFor = Exception.class)
    @Override
    public void create(Order order) {
        //创建订单
        log.info("--------------------》开始创建订单");
        orderDao.create(order);

        //扣减库存
        log.info("---------------------》订单微服务开始调用库存，做扣减Count");
        storageService.decrease(order.getProductId(),order.getCount());
        log.info("---------------------》订单微服务开始调用库存，扣减end");

        //扣减账户
        log.info("---------------------》订单微服务开始调用账户，做扣减Money");
        accountService.decrease(order.getUserId(),order.getMoney());
        log.info("---------------------》订单微服务开始调用账号，扣减Money   end");


        //修改订单状态   从零到1代表已经完成
        log.info("--------------------》修改订单状态开始");
        orderDao.update(order.getUserId(),0);
        log.info("---------------------》修改订单状态结束");

        log.info("-------------------------》下订单事务完成。。。。。。。。。。。。");

    }
}
